﻿#region Copyright
// Copyright 2008 Chris Varley (http://www.contractdriven.net/)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#endregion

using System;
using System.Globalization;

namespace ContractDriven.CommonAssertions
{
    #region StringNotContainsAssertion
    /// <summary>
    /// Describes a guarantee that a string contains a given sub-string.
    /// </summary>
    public class StringContainsAssertion : IAssertion
    {
        #region Members
        public string ExpectedSubString { get; private set; }
        public string ActualString { get; private set; }
        public StringComparison ComparisonType { get; private set; }
        #endregion

        #region Constructors

        /// <summary>
        /// Describes a guarantee that a string contains a given sub-string, using the given comparison type.
        /// </summary>
        /// <param name="expectedSubString">The sub-string expected to be in the string</param>
        /// <param name="actualString">The string to be inspected</param>
        /// <param name="comparisonType">The type of string comparison to perform</param>
        public StringContainsAssertion(string expectedSubString, string actualString, StringComparison stringComparison)
        {
            Contract.Require(Assert.IsNotNull(expectedSubString));
            Contract.Require(Assert.IsNotNull(actualString));

            ExpectedSubString = expectedSubString;
            ActualString = actualString;
            ComparisonType = stringComparison;
        }


        #endregion


        #region IAssertion Members

        string IAssertion.FailureDescription
        {
            get { return string.Format(CultureInfo.CurrentCulture, Resource.StringNotContainsAssertion, ExpectedSubString ); }
        }

        public bool Eval()
        {
            return (ActualString.IndexOf(ExpectedSubString, ComparisonType) > -1);
        }

        #endregion
    }
    #endregion
}
